package com.jianbing.controller;

import com.jianbing.dao.MongoDBDao;
import com.jianbing.entity.Customer;
import com.jianbing.entity.CustomerProduct;
import com.jianbing.entity.Result;
import com.jianbing.exception.ServiceException;
import com.jianbing.util.StringUtils;
import com.jianbing.util.TableUtil;
import com.mongodb.BasicDBObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * \* Created with IntelliJ IDEA. \* User: LFL \* Date: 2017/4/24 \* Time: 20:47
 * \* To change this template use File | Settings | File Templates. \*
 * Description: \
 */

@Controller
@RequestMapping(value = "/customerProduct")
public class CustomerProductController {
	Logger log = Logger.getLogger(CustomerProductController.class);

	/**
	 * 
	 * 添加客戶购买的产品
	 * 
	 * @author cwl
	 * @date 2017年5月12日
	 *
	 * @param request
	 * @param response
	 * @param customized
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Result addCustomerProduct(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute CustomerProduct customized) {
		Result result = new Result();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		customized.setUpdateTime(dateFormat.format(new Date()));
		customized.setCurrenTimes(0);
		if (StringUtils.isEmpty(customized.getAppKey()) || StringUtils.isEmpty(customized.getGoodsSn())) {
			result.setCode(-1);
			result.setMsg("参数错误");
			return result;
		}
		try {
			BasicDBObject queryObject = new BasicDBObject();
			queryObject.append("appKey", customized.getAppKey());
			Customer customer = MongoDBDao.findOne(TableUtil.customer, Customer.class, queryObject);
			if (customer == null) {
				result.setCode(-1);
				result.setMsg("用户不存在");
				return result;
			}
			MongoDBDao.insert(TableUtil.customer_product, customized);
		} catch (ServiceException e) {
			log.error(e.getMessage(), e);
			result.setCode(-1);
			result.setMsg("系统异常");
		}
		return result;
	}

	/**
	 * 
	 * 查询客戶购买的产品
	 * 
	 * @author cwl
	 * @date 2017年5月12日
	 *
	 * @param customized
	 * @return
	 */
	@SuppressWarnings("static-access")
	@RequestMapping(value = "/query", method = RequestMethod.GET)
	@ResponseBody
	public Result queryCustomized(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute CustomerProduct customized) {
		Result result = new Result();
		ModelMap model = new ModelMap();
		BasicDBObject queryObject = new BasicDBObject();
		if (!StringUtils.isEmpty(customized.getAppKey())) {
			queryObject.append("appKey", customized.getAppKey());
		}
		if (!StringUtils.isEmpty(customized.getGoodsSn())) {
			queryObject.append("goodsSn", customized.getGoodsSn());
		}

		try {
			MongoDBDao dbDao = new MongoDBDao();
			List<CustomerProduct> list = dbDao.queryForList(TableUtil.customer_product, CustomerProduct.class,
					queryObject, null, null, null, null);
			model.addAttribute("list", list);
			result.setData(model);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.setCode(-1);
			result.setMsg("系统异常");
		}
		return result;
	}

	/**
	 * 
	 * 修改客戶购买的产品
	 * 
	 * @author cwl
	 * @date 2017年5月12日
	 *
	 * @param customized
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Result updateCustomized(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute CustomerProduct customized) {
		Result result = new Result();
		if (StringUtils.isEmpty(customized.getGoodsSn()) || StringUtils.isEmpty(customized.getAppKey())) {
			result.setCode(-1);
			result.setMsg("参数错误");
			return result;
		}
		try {
			BasicDBObject queryObject = new BasicDBObject();
			queryObject.append("appKey", customized.getAppKey()).append("goodsSn", customized.getGoodsSn());
			long count = MongoDBDao.update(TableUtil.customer_product, queryObject, customized);
			if (count <= 0) {
				result.setCode(-1);
				result.setMsg("更新失败或记录不存在");
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.setCode(-1);
			result.setMsg("系统异常");
		}

		return result;
	}

	/**
	 * 
	 * 刪除客戶购买的产品
	 * 
	 * @author cwl
	 * @date 2017年5月12日
	 *
	 * @param customized
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	@ResponseBody
	public Result deleteCustomized(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute CustomerProduct customized) {
		Result result = new Result();
		if (StringUtils.isEmpty(customized.getAppKey()) || StringUtils.isEmpty(customized.getGoodsSn())) {
			result.setCode(-1);
			result.setMsg("参数错误");
			return result;
		}

		try {
			BasicDBObject queryObject = new BasicDBObject();
			queryObject.append("appKey", customized.getAppKey());
			queryObject.append("goodsSn", customized.getGoodsSn());
			MongoDBDao.delete(TableUtil.customer_product, queryObject);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.setCode(-1);
			result.setMsg("系统异常");
		}
		return result;
	}

	/**
	 * 
	 * 刪除客戶所有买的产品
	 * 
	 * @author cwl
	 * @date 2017年5月12日
	 *
	 * @param customized
	 * @return
	 */
	@RequestMapping(value = "/deleteAll")
	@ResponseBody
	public Result deleteAllCustomized(@ModelAttribute CustomerProduct customized) {
		Result result = new Result();
		if (StringUtils.isEmpty(customized.getAppKey())) {
			result.setCode(-1);
			result.setMsg("参数错误");
			return result;
		}

		try {
			BasicDBObject queryObject = new BasicDBObject();
			queryObject.append("appKey", customized.getAppKey());
			MongoDBDao.deleteMany(TableUtil.customer_product, queryObject);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.setCode(-1);
			result.setMsg("系统异常");
		}
		return result;
	}

}
